package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.ExamService;
import cc.eddic.pratice.programmingparadigms.exam.MasterDataService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.domain.Answer;
import cc.eddic.pratice.programmingparadigms.exam.domain.Exam;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
public class ExamServiceZyzStreamImpl implements ExamService {
    @Override
    public List<Answer> importAnswers(String answerExcelFile) {
        MasterDataService masterDataService = new MasterDataServiceGcsImpl();
        QuestionBankService questionBankService = new QuestionBankServiceGcsImpl();

        List<Answer> listAnswers = new ArrayList<>();

        Exam exam = masterDataService.getExams().stream().filter(
                x -> "7-12 开营考试".equals(x.getName())).findFirst().orElseThrow(
                () -> new IllegalArgumentException("找不到 7-12 开营考试")
        );

        List<Question> listQuestions = questionBankService.importCorrectAnswers("");
        List<Student> listStudents = masterDataService.getStudents();

        try {
            FileInputStream file = new FileInputStream(answerExcelFile);
            Workbook workbook = new XSSFWorkbook(file);
            Sheet answerSheet = workbook.getSheetAt(1);

            Iterator<Row> rowIterator = answerSheet.rowIterator();
            Row rowStudent = rowIterator.next();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                Cell cellNo = cellIterator.next();
                int no = (int) cellNo.getNumericCellValue();
                Question question = listQuestions.stream().filter(q -> q.getNo() == no).findFirst().orElseThrow
                        (() -> new IllegalArgumentException("Question no " + no + " No exist"));

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    int columnIndexCell = cell.getColumnIndex();
                    String nameStudent = rowStudent.getCell(columnIndexCell).getRichStringCellValue().getString();
                    Student student = listStudents.stream().filter(st -> nameStudent.equals(st.getName())).findFirst()
                            .orElseThrow(() -> new IllegalArgumentException("No student " + nameStudent));

                    String strCell = cell.getRichStringCellValue().getString();
                    char choice = strCell.isEmpty() ? '*' : strCell.charAt(0);

                    Answer answer = new Answer(exam, question, student, choice);
                    listAnswers.add(answer);
                }
            }

            return listAnswers;

        } catch (FileNotFoundException e) {
            log.error("importCategories FileInputStream FileNotFoundException\n");
        } catch (IOException e) {
            log.error("importCategories XSSFWorkbook IOException\n");
        }

        return null;

    }
}
